import { ref } from "vue";
import { defineStore } from "pinia";
import type { DeviceInfo } from "../api/manualTest/index.d";
export const useDeviceViewStore = defineStore("deviceViewStore", () => {

    /**
     * 存储当前选中设备的所有指令信息
     */
    const currentCommandList = ref([]);
    const storeCommandList = (commands: any) => {
        currentCommandList.value = commands;
    }

    /**
     * 存储当前点击的设备
     */
    const currentDevice = ref<DeviceInfo>();
    const setCurrentDevice = (device: DeviceInfo) => {
        currentDevice.value = device;
    }

    /**
     * 存储所有的设备信息
     */
    const allDevices = ref<DeviceInfo[]>([]);
    const setAllDevices = (devices: DeviceInfo[]) => {
        allDevices.value = devices;
    }

    /**
     * 将点击的设备信息存储
     */
    const clickedDevice = ref<any[]>([]);
    const setClickedDevice = (device: any) => {
        if (clickedDevice.value.findIndex(x => x.id === device.id) === -1) {
            clickedDevice.value.push(device);
        }
    }
    /**
     * 
     * @param device 删除的设备信息
     */
    const removeClickedDevice = (device: any) => {
        const index = clickedDevice.value.findIndex(x => x.id === device.id);
        if (index !== -1) {
            clickedDevice.value.splice(index, 1);
        }
    }

    /**
     * 存储关闭的设备信息 然后设备列表用于监听变化获取设备列表
     */
    const closedDevice = ref<any>();
    const setClosedDevice = (device: any) => {
        closedDevice.value = device;
    }

    /**
     * 设置前两个指令信息，设备可能是Open/Close或者是打开/关闭，因此保存前两个指令
     */
    const openCloseMethodName = ref<string[]>([]);
    const setOpenCloseMethodName = (methodName: string) => {
        if (!openCloseMethodName.value?.includes(methodName)) {
            openCloseMethodName.value.push(methodName);
        }
    }

    /**
     * 存储当前设备的状态
     */
    const currentDeviceConnStatus = ref<any>();
    const setCurrentDeviceConnStatus = (deviceWithStatus: any) => {
        currentDeviceConnStatus.value = deviceWithStatus;
    }

    return {
        currentCommandList,
        storeCommandList,
        currentDevice,
        setCurrentDevice,
        allDevices,
        setAllDevices,
        clickedDevice,
        setClickedDevice,
        removeClickedDevice,
        closedDevice,
        setClosedDevice,
        openCloseMethodName,
        setOpenCloseMethodName,
        currentDeviceConnStatus,
        setCurrentDeviceConnStatus
    }
})